package lib.bone.admin.controller.dict;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dao.NepuTopicDictDAO;
import lib.bone.access.module.dict.dto.NepuTopicDictAddDTO;
import lib.bone.access.module.dict.dto.NepuTopicDictQueryDTO;
import lib.bone.access.module.dict.dto.NepuTopicDictUpdateDTO;
import lib.bone.access.module.dict.entity.NepuTopicDict;
import lib.bone.access.module.dict.DictServiceCtl;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author : cg
 * @Date : 2021-1-12
 */

@Slf4j
@RestController
@RequestMapping("nepu/topic")
@Api(tags = "纵向课题词典")
public class NepuTopicDictController {
    @Autowired
    DictServiceCtl dictServiceCtl;

    @Autowired
    NepuTopicDictDAO nepuTopicDictDAO;


    @GetMapping("dicts")
    @ApiOperation(value = "查询纵向课题词典")
    public ResponseDTO getDicts(NepuTopicDictQueryDTO nepuTopicDictQueryDTO){
        Sort sort;
        if(nepuTopicDictQueryDTO.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, nepuTopicDictQueryDTO.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, nepuTopicDictQueryDTO.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(nepuTopicDictQueryDTO.getPageNum() - 1 , nepuTopicDictQueryDTO.getPageSize(), sort);

        NepuTopicDict nepuTopicDict = new NepuTopicDict();
        BeanUtil.copyProperties(nepuTopicDictQueryDTO,nepuTopicDict, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<NepuTopicDict> example = Example.of(nepuTopicDict);
        return ResponseDTO.succData(dictServiceCtl.getAll(pageable,example, ResearchTypeConst.TOPIC));

     //   return nepuTopicDictService.getAll(pageable,example);
    }

    @WebLog(module = "nepu-access")
    @PostMapping(value = "dicts")
    @ApiOperation(value = "新建记录",notes = "")
    public ResponseDTO saveOne(@RequestBody @Validated NepuTopicDictAddDTO nepuTopicDictAddDTO){
        NepuTopicDict dict = new NepuTopicDict();
        BeanUtil.copyProperties(nepuTopicDictAddDTO,dict,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return dictServiceCtl.saveOne(dict);    }

    @GetMapping("dicts/{id}")
    @ApiOperation(value = "查询单条记录",notes = "传入ID")
    public ResponseDTO getOne(@PathVariable("id") String id){
        Optional<Object> nepuTopicDictOp = dictServiceCtl.getById(id,ResearchTypeConst.TOPIC);
        return nepuTopicDictOp.<ResponseDTO>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND, "ID:[" + id + "]对应的数据不存在"));

    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.DELETE)
    @DeleteMapping("dicts/{id}")
    @ApiOperation(value = "删除单条记录",notes = "传入ID")
    public ResponseDTO deleteOne(@PathVariable("id") String id){
        return dictServiceCtl.deleteById(id,ResearchTypeConst.TOPIC);
    }

    @GetMapping("dicts/type")
    @ApiOperation(value = "记录",notes = "传入ID")
    public ResponseDTO searchType(){
        Sort sort;
        sort = Sort.by(Sort.Direction.DESC,"topicLevel");
        List<NepuTopicDict> list = nepuTopicDictDAO.findAll(sort);
        Map<String,String> map = new HashMap<>();
        for (NepuTopicDict nepuTopicDict : list) {
            map.put(nepuTopicDict.getTopicType(),nepuTopicDict.getTopicLevel());
        }
        return ResponseDTO.succData(map);

    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.UPDATE)
    @PutMapping("dicts")
    @ApiOperation(value = "更新单条记录",notes = "")
    public ResponseDTO updateOne(@RequestBody @Validated NepuTopicDictUpdateDTO nepuTopicDictUpdateDTO){
        Optional<Object> dictOp = dictServiceCtl.getById(nepuTopicDictUpdateDTO.getId(),ResearchTypeConst.TOPIC);
        if (!dictOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        }
        NepuTopicDict dict = (NepuTopicDict) dictOp.get();
        BeanUtil.copyProperties(nepuTopicDictUpdateDTO,dict,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return dictServiceCtl.updateOne(dict,ResearchTypeConst.TOPIC);    }


//    @GetMapping(value = {"/dicts"})
//    @ApiOperation(value = "查询全部",notes = "")
//    public ResponseDTO getAll(){
//        return nepuTopicDictService.getAll();
//    }
//
//
//    @GetMapping(value = {"/getOneByTopicType"})
//    @ApiOperation(value = "按类型查询课题成果",notes = "传入topicType，获取纵向课题信息")
//    public ResponseDTO getOneByPaperType(String topicType){
//        return nepuTopicDictService.getByTopicType(topicType);
//    }
//
//    @GetMapping(value = {"/getOneByTopicLevel"})
//    @ApiOperation(value = "按级别查询课题成果",notes = "传入topicLevel，获取纵向课题信息")
//    public ResponseDTO getOneByPaperLevel(String topicLevel){
//        return nepuTopicDictService.getByTopicLevel(topicLevel);
//    }
//
//    @GetMapping(value = {"/delOneById/{id}"})
//    @ApiOperation(value = "删除单个课题成果",notes = "传入课题成果id，删除课题信息")
//    public ResponseDTO deleteOneById(@PathVariable("id") String id){
//        return nepuTopicDictService.deleteById(id);
//    }
//
//    @PostMapping(value = {"nepuPaperDict"})
//    @ApiOperation(value = "新建一个课题成果",notes = "")
//    public ResponseDTO savePaperDict(@RequestBody NepuTopicDict nepuTopicDict){
//        return nepuTopicDictService.save(nepuTopicDict);
//    }
//
//    @PutMapping(value = {"/topicdict"})
//    @ApiOperation(value = "更新一条数据",notes = "")
//    public ResponseDTO updatePaperDict(@RequestBody NepuTopicDict nepuTopicDict){
//        return nepuTopicDictService.save(nepuTopicDict);
//    }




}
